
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Syrinx.Gui.AspNet
{
    public class DiggPaginationDisplay : Swaf.Gui.AspNet2.BasicPaginationDisplay
    {


        protected override void Render(HtmlTextWriter w)
        {
            bool drawGroupMove = NumPages > GroupSize;

            w.Write("<div>");
            if (m_prefixInfoHtml != null && m_prefixInfoHtml != "")
                w.Write("<span class=\"{0}\">{1}</span>", m_prefixClass, m_prefixInfoHtml);

            // the current page not zero index
            int curPage = m_currentPage + 1;

            if (GroupSize < 1) 
                GroupSize = 1;
            int mid = GroupSize / 2;

            string skip = string.Format("<span class=\"{0}\">...</span>",m_pagesClass);


            //// Previous link
            if (drawGroupMove)
            {
                if (curPage - 1 > 0)
                    w.Write(string.Format("<span onclick=\"{0}\" class=\"{1}\">{2}</span>", genPostBack((m_currentPage - 1).ToString()), m_groupingClass, m_prevGroupHtml));
                else
                    w.Write(string.Format("<span class=\"disabled\">{0}</span>", m_prevGroupHtml));
            }


            //// First number 
            if (curPage == 1)
                w.Write(string.Format("<span class=\"{0}\">{1}</span>", m_currentPageClass, 1));
            else
            {
                string tmp="";
                if(curPage-mid > 2)
                    tmp = skip;
                w.Write(string.Format("<span onclick=\"{0}\" class=\"{1}\">{2}</span>{3}", genPostBack((0).ToString()), m_pagesClass, 1, tmp));
            }


            int start = curPage - mid;
            if ( curPage > (NumPages-GroupSize) ) 
                start = NumPages - GroupSize; 
           
            if(start<=1)
                start = 2;


            int ctr=0;
            for(int i = start; i <= NumPages-1;i++)
            {
                if(ctr>= GroupSize)
                    break;

                if( i == curPage)
                    w.Write(string.Format("<span class=\"{0}\">{1}</span>", m_currentPageClass, i));
                else
                     w.Write(string.Format("<span onclick=\"{0}\" class=\"{1}\">{2}</span>", genPostBack((i-1).ToString()), m_pagesClass, i));

                ctr++;
            }
            

            //// Last number 
            if (NumPages > 1)
            {
                if (curPage == NumPages)
                    w.Write(string.Format("<span class=\"{0}\">{1}</span>", m_currentPageClass, NumPages));
                else 
                {
                    string tmp="";
                    if(start+GroupSize <= NumPages-2)
                        tmp = skip;
                    w.Write(string.Format("{3}<span onclick=\"{0}\" class=\"{1}\">{2}</span>", genPostBack((NumPages-1).ToString()), m_pagesClass, NumPages, tmp));
                }
            }

            if (drawGroupMove)
            {
                // next link
                if (curPage < NumPages)
                    w.Write(string.Format("<span onclick=\"{0}\" class=\"{1}\">{2}</span>", genPostBack((m_currentPage + 1).ToString()), m_groupingClass, m_nextGroupHtml));
                else
                    w.Write(string.Format("<span class=\"disabled\">{0}</span>", m_nextGroupHtml));
            }
            w.Write("</div>");
        }

    }
}
